package com.yulebaby.callsenter.task;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.orhanobut.logger.Logger;
import com.yulebaby.callsenter.bean.ActualTranProgressBean;
import com.yulebaby.callsenter.bean.TranProgressTargetBean;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.http.ServerUrls;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.ui.activity.LoginActivity;
import com.yulebaby.callsenter.utils.SPUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

import java.util.List;

/**
 * Author: Zhangbo
 * Time: 2017/1/22
 */
public class UploadTransDailyTask {
    /**
     * 添加转化目标数
     */
    public static void doUploadPreSaleRemark(Activity activity, List<TranProgressTargetBean> beanList, HttpCallback httpCallback) {
        if (beanList != null) {
            HttpParams httpParams = new HttpParams();
            String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
            int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
            if (checkNull(user_token, activity)) {
                httpParams.put("token", user_token);
                httpParams.put("sale_id", user_id);
                String targetJson = new Gson().toJson(beanList);
                httpParams.put("target", targetJson);
                HttpHelper.getInstance().post(activity, ServerUrls.OPT_SAVETRANTARGET, httpParams, httpCallback);
            }
        }
    }

    public static void doUploadUnfinishedCause(Activity activity, int track_pro, String cause, HttpCallback httpCallback) {
        HttpParams httpParams = new HttpParams();
        String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
        int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
        if (checkNull(user_token, activity)) {
            httpParams.put("token", user_token);
            httpParams.put("sale_id", user_id);
            httpParams.put("track_pro", track_pro);
            httpParams.put("unfinished_reason", cause);
            HttpHelper.getInstance().post(activity, ServerUrls.OPT_SAVEUNFINISHEDREASON, httpParams, httpCallback);
        }
    }

    /**
     * 判断是否为空
     *
     * @param string
     * @return
     */
    private static boolean checkNull(String string, Activity mActivity) {
        if (TextUtils.isEmpty(string)) {
            ToastUtil.showToast("登录信息出错,请重新登录");
            //TOKEN 失效跳转到登录界面
            SPUtil.remove(Constant.USER_TOKEN); //对登录信息进行清除
            SPUtil.remove(Constant.USER_ID);
            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
            mActivity.finish();
            return false;
        }
        return true;
    }
}
